package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.emq.Topic;
import com.lkd.service.VendingMachineService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author : Bing
 * @Date 2023/3/2 18:52
 */
@Component
@Topic(TopicConfig.VMS_COMPLETED_TOPIC)
public class CompleteOpsHandler implements MsgHandler {

    @Autowired
    private VendingMachineService vendingMachineService;

    /**
     * 运维工单修改售货机状态
     *
     * @param jsonMsg EMQ消息
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {

        //解析消息
        TaskCompleteContract contract = JsonUtil.getByJson(jsonMsg, TaskCompleteContract.class);

        //投放工单：完成后将售货机状态更改为  运营 状态
        if (contract.getTaskType() == VMSystem.TASK_TYPE_DEPLOY) {
            vendingMachineService.updateStatus(contract.getInnerCode(), VMSystem.VM_STATUS_RUNNING);
        }

        //撤机工单：完成后将售货机状态更改为  撤机 状态
        if (contract.getTaskType() == VMSystem.TASK_TYPE_REVOKE) {
            vendingMachineService.updateStatus(contract.getInnerCode(), VMSystem.VM_STATUS_REVOKE);
        }
    }

}
